package com.efast.dmom.mgmt.openws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cafeauto.ws.Message;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.base.WSAbstractAdapter;
import com.efast.cafe.commponent.es.webservice.utils.W3CElementUtil;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.mes.testline.FourWheelPositionData;
import com.efast.dmom.mgmt.bean.mes.testline.FourWheelPositionDataBean;
import com.efast.dmom.mgmt.bean.mes.testline.FourWheelPositionDataBizContent;
import com.efast.dmom.mgmt.bean.mes.testline.FourWheelPositionInboundDataSet;
import com.efast.dmom.mgmt.bean.mes.testline.TestLineDataLogBean;
import com.efast.dmom.mgmt.service.mes.testline.impl.TestLineService;

@CafeWSService(bizCode = "FourWheelPosition_Test")
public class FourWheelPositionDataService extends WSAbstractAdapter {

	@Override
	public Message handleMessage(Message message) {
		
		logger.info("Received FourWheelPosition data from: " + message.getBIZ_CODE());

		FourWheelPositionDataBizContent content = W3CElementUtil.elementUnmarshalToObj(message.getBIZ_CONTENT(),
				FourWheelPositionDataBizContent.class);
		FourWheelPositionInboundDataSet inboundData = content.getFourWheelPositionInboundDataSet();
		List<FourWheelPositionData> dataList = inboundData.getFourWheelPositionData();

		if(dataList != null && dataList.size() >0){
			
			List<FourWheelPositionDataBean> list = new ArrayList<FourWheelPositionDataBean>();
			for (FourWheelPositionData data : dataList) {

				FourWheelPositionDataBean dataBean = new FourWheelPositionDataBean();

				dataBean.setVin(data.getVin());
				dataBean.setTime(data.getTime());
				dataBean.setToeInValue(data.getToeInValue());
				dataBean.setTwoAxisLeftDirectionAngle(data.getTwoAxisLeftDirectionAngle());
				dataBean.setTwoAxisRightDirectionAngle(data.getTwoAxisRightDirectionAngle());
				dataBean.setOneAxisLeftDirectionAngle(data.getOneAxisLeftDirectionAngle());
				dataBean.setOneAxisRightDirectionAngle(data.getOneAxisRightDirectionAngle());
				dataBean.setFourWheelPositioningResult(data.getFourWheelPositioningResult());
				dataBean.setTestNumber(data.getTestNumber());
				dataBean.setCreateTime(new Date());
				dataBean.setCreateUser(message.getBIZ_CODE());

				list.add(dataBean);
			}

			TestLineService service = SpringContextUtils.getBean(TestLineService.class);
			service.insertFourWheelPositionData(list);
			
			TestLineDataLogBean logBean = new TestLineDataLogBean();
			logBean.setVin(dataList.get(0).getVin());
			logBean.setStatus("保存");
			logBean.setReceivedTime(new Date());
			logBean.setSource(message.getMESSAGE_FROM());
			logBean.setDestination(message.getMESSAGE_TO());
			
			service.insertTestLineLog(logBean);
		}
		
		message.setBIZ_CONTENT(null);
		message.setMESSAGE_FROM("MES");
		message.setMESSAGE_TO("FourWheelPosition_Test");
		message.setRET_CODE("200");
		message.setRET_CODE_DESC("Successful");
		return message;
	}

}
